#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
import json
import multiprocessing
from collections import defaultdict
from pytools import mysqltool
from pytools.hbaseservice import HBaseService


def dump_data_to_json(filename, obj):
    fullname = os.path.join('data', filename)
    with open(fullname, 'ab') as f:
        json.dump(obj,f)
        f.write('\n')


def get_day_summary(hbase, table, day):
    rowKey = 'A_' + day
    summary = hbase.queryByRowKey(table, rowKey)
    if len(summary) != 0:
        # print summary
        dump_data_to_json(day + '_day_summary', summary)
    return summary


def get_summary_by_typeid(hbase, table, day, cate, local, typeids):
    keyrowlist = []
    for typeid in typeids:
        keyRow = 'cate%s%s_local%s%s_%s_%s' % (cate[0], cate[1], local[0], local[1], typeid, day)
        keyrowlist.append(keyRow)

    return hbase.queryMultiByRowKeyList(table, keyrowlist)


def get_cate_local_summary(hbase, table, day, cate, local):
    # validtiezi = 'cate%s%s_local%s%s_state0_%s' % (cate[0], cate[1], local[0], local[1], day)
    # newtiezi = 'cate%s%s_local%s%s_uptype0_%s' % (cate[0], cate[1], local[0], local[1], day)
    # modifytiezi = 'cate%s%s_local%s%s_uptype1_%s' % (cate[0], cate[1], local[0], local[1], day)
    # refreshtiezi = 'cate%s%s_local%s%s_uptype2_%s' % (cate[0], cate[1], local[0], local[1], day)

    typeids = ['state0', 'uptype0', 'uptype1', 'uptype2']
    result = get_summary_by_typeid(hbase, table, day, cate, local, typeids)
    if len(result) != 0:
        # print result
        dump_data_to_json(day + '_cate_local_summary', result)
    return result


def get_personal_summary(hbase, table, day, cate, local):
    # validtiezi = 'cate%s%s_local%s%s_state0_source0_%s' % (cate[0], cate[1], local[0], local[1], day)
    # newtiezi = 'cate%s%s_local%s%s_uptype0_source0_%s' % (cate[0], cate[1], local[0], local[1], day)
    # modifytiezi = 'cate%s%s_local%s%s_uptype1_source0_%s' % (cate[0], cate[1], local[0], local[1], day)
    # refreshtiezi = 'cate%s%s_local%s%s_uptype2_source0_%s' % (cate[0], cate[1], local[0], local[1], day)

    typeids = ['state0_source0', 'uptype0_source0', 'uptype1_source0', 'uptype2_source0']
    result = get_summary_by_typeid(hbase, table, day, cate, local, typeids)
    if len(result) != 0:
        # print result
        dump_data_to_json(day + '_personal_summary', result)
    return result


def get_vip_summary(hbase, table, day, cate, local):
    # validtiezi = 'cate%s%s_local%s%s_state0_source6_%s' % (cate[0], cate[1], local[0], local[1], day)
    # newtiezi = 'cate%s%s_local%s%s_uptype0_source6_%s' % (cate[0], cate[1], local[0], local[1], day)
    # modifytiezi = 'cate%s%s_local%s%s_uptype1_source6_%s' % (cate[0], cate[1], local[0], local[1], day)
    # refreshtiezi = 'cate%s%s_local%s%s_uptype2_source6_%s' % (cate[0], cate[1], local[0], local[1], day)

    typeids = ['state0_source6', 'uptype0_source6', 'uptype1_source6', 'uptype2_source6']
    result = get_summary_by_typeid(hbase, table, day, cate, local, typeids)
    if len(result) != 0:
        # print result
        dump_data_to_json(day + '_vip_summary', result)
    return result


def get_depth_info(q):
    dbh = mysqltool.connect_to_58_stats()
    result = mysqltool.query(dbh, q)

    depthname = {'0': 'first', '1': 'second', '2': 'third'}

    depthinfo = defaultdict(list)
    for id, depth in result:
        depthinfo[depthname[depth]].append(id)

    return dict(depthinfo)


def get_cate_upto_level_2():
    q = 'select cateid, depth from t_category where depth < 2'
    # q += ' limit 1'
    result = get_depth_info(q)
    print result
    return result


def get_local_upto_level_3():
    q = 'select displocalid, depth from t_displocal where depth < 3'
    # q += ' limit 1'
    result = get_depth_info(q)
    print result
    return result


def multiproc_get_day_summary(func2call, hbase, table, day):
    try:
        hbase = HBaseService()
        func2call(hbase, table, day)
    except KeyboardInterrupt:
        print '\n\nKeyboardInterrupt, return\n\n'
        return


def multiprocfunc(cateinfo, localinfo, func2call, hbase, table, day):
    try:
        hbase = HBaseService()
        for catelevel, catenums in cateinfo.items():
            for catenum in catenums:
                cate = [catelevel, catenum]
                for locallevel, localnums in localinfo.items():
                    for localnum in localnums:
                        local = [locallevel, localnum]
                        # print cate, local
                        func2call(hbase, table, day, cate, local)
    except KeyboardInterrupt:
        print '\n\nKeyboardInterrupt, return\n\n'
        hbase.shutdown()
        return


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'usage: %s <20120313>' % (sys.argv[0])
        sys.exit(1)

    # day = '20121112'
    day = sys.argv[1]

    table = 'FourthInfo'
    # hbase = HBaseService()
    # get_day_summary(hbase, table, day)
    # hbase.shutdown()

    cateinfo = get_cate_upto_level_2()
    localinfo = get_local_upto_level_3()

    # the HBaseService coun't fork, but we keep it as args
    hbase = None

    pool = multiprocessing.Pool(processes=4)
    pool.apply_async(multiproc_get_day_summary, (get_day_summary, hbase, table, day))

    pool.apply_async(multiprocfunc, (cateinfo, localinfo, get_cate_local_summary, hbase, table, day))
    pool.apply_async(multiprocfunc, (cateinfo, localinfo, get_personal_summary, hbase, table, day))
    pool.apply_async(multiprocfunc, (cateinfo, localinfo, get_vip_summary, hbase, table, day))
    pool.close()

    pool.join()

